package com.example.queryexpressdelivery_01.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.common.DeliveryDAL;
import com.example.queryexpressdelivery_01.entity.FeedbackData;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.adapter.ReplyAdapter;
import com.example.queryexpressdelivery_01.entity.UserData;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsActivity extends AppCompatActivity {

    private RecyclerView rvDetailReply;
    private ImageView ivDetailReturn;
    private DeliveryDAL deliveryDAL = new DeliveryDAL(this);
    private Gson gson = new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_details);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
// 在 UserDetailsActivity 的 onCreate 方法中
        RecyclerView recyclerView = findViewById(R.id.rvDetailReply);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        int userId = getIntent().getIntExtra("id", -1); // 获取用户ID
        List<String> questions = getQuestionsFromSomewhere();
        ReplyAdapter adapter = new ReplyAdapter(questions, this, userId); // 传递用户ID
        recyclerView.setAdapter(adapter);
        ivDetailReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(UserDetailsActivity.this, AdminActivity.class));
                finish();
            }
        });

    }
    private List<String> getQuestionsFromSomewhere() {
        // ...
        UserData userData = deliveryDAL.queryById(getIntent().getIntExtra("id",-1));
        if (userData!=null){
            if (userData.getData()!=null){
                FeedbackData feedbackData = gson.fromJson(userData.getData(),FeedbackData.class);
                if (feedbackData.getProblemfeedback()!=null){
                    List<String> problems = new ArrayList<>();
                    for (int i=0;i<feedbackData.getProblemfeedback().size();i++){
                        if (feedbackData.getProblemfeedback().get(i).getRecover()==null){
                            problems.add("用户反馈："+feedbackData.getProblemfeedback().get(i).getProblem());
                        }
                    }
                    return problems;
                }
            }
        }
        return new ArrayList<>();
    }
    private void initView() {
        rvDetailReply = (RecyclerView) findViewById(R.id.rvDetailReply);
        ivDetailReturn = (ImageView) findViewById(R.id.ivDetailReturn);
    }
}